package cn.edu.neusoft.zengweidong707.neufood.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.activity.FoodDetailsActivity;
import cn.edu.neusoft.zengweidong707.neufood.activity.InsertCommentActivity;
import cn.edu.neusoft.zengweidong707.neufood.bean.UserOrderBean;

/**
 * Created by Lucio on 2017/12/20.
 */

public class UserOrderAdapter extends BaseAdapter<UserOrderBean> {
    public UserOrderAdapter(Context context, List<UserOrderBean> items) {
        super(context, items);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.items_food_order, parent, false);
        UserOrderViewHolder userOrderViewHolder = new UserOrderViewHolder(itemView);
        return userOrderViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UserOrderBean entity = (UserOrderBean) items.get(position);
        if (null == entity)
            return;
        UserOrderViewHolder myViewHolder = (UserOrderViewHolder)holder;

        myViewHolder.ord_foodname.setText(entity.getFoodname());
        Double prices = entity.getPrice();
        myViewHolder.ord_price.setText(prices.toString());
        String num = String.valueOf(entity.getNum());
        myViewHolder.ord_num.setText(num);
        myViewHolder.ord_address.setText(entity.getShopaddress());
        myViewHolder.ord_shopname.setText(entity.getShopname());
        myViewHolder.ord_time.setText(entity.getOrdertime());

        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int foodid = entity.getFood_id();
                Intent intent = new Intent(context, FoodDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("foodid",foodid);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        myViewHolder.btn_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != entity.getContent())
                {
                    Toast.makeText(context,"该订单已经评价！",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Intent intent = new Intent(context, InsertCommentActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("orderid",entity.getOrder_id());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });

    }

    public class UserOrderViewHolder extends RecyclerView.ViewHolder {
        TextView ord_shopname;
        TextView ord_time;
        TextView ord_foodname;
        TextView ord_num;
        TextView ord_price;
        TextView ord_address;
        Button btn_comment;
        UserOrderViewHolder(View itemView) {
            super(itemView);
            btn_comment = (Button) itemView.findViewById(R.id.btn_comment);
            ord_address = (TextView) itemView.findViewById(R.id.ord_address);
            ord_shopname = (TextView) itemView.findViewById(R.id.ord_shopname);
            ord_time = (TextView) itemView.findViewById(R.id.ord_time);
            ord_foodname = (TextView) itemView.findViewById(R.id.ord_foodname);
            ord_num = (TextView) itemView.findViewById(R.id.ord_num);
            ord_price = (TextView) itemView.findViewById(R.id.ord_price);
        }
    }
}
